home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Toolbox / Live Scroll 1.0 / Sources / ScrollBars.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-13  |  5.5 KB  |  285 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Lists.c
  3.  
  4.     Contains:    List Manager stuff and associated routines
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.             03/29/96            CW        First release
  13.  
  14. */
  15.  
  16.  
  17. #pragma segment Core
  18.  
  19.  
  20. // System Includes
  21.  
  22.  
  23.  
  24. // Application Includes
  25.  
  26. #ifndef __BAREBONES__
  27.     #include "BareBones.h"
  28. #endif
  29.  
  30.  
  31. #ifndef __PROTOTYPES__
  32.     #include "Prototypes.h"
  33. #endif
  34.  
  35.  
  36.  
  37.  
  38.  
  39. static ControlRef   gControl;
  40. static SInt32        gValueSlop;
  41. static SInt32        gSaveValue;
  42. static SInt32        gStartValue;
  43. static RgnHandle    gSaveClip = nil;
  44.  
  45.  
  46.  
  47. static SInt32    CalcValueFromPoint ( ControlRef theControl, Point thePoint );
  48. static void        DisableDrawing ( void );
  49. static void        EnableDrawing ( void );
  50.  
  51.  
  52.  
  53.  
  54.  
  55. OSErr BeginThumbTracking ( ControlRef theControl )
  56. {
  57.     const SInt32 kMinLowMem    = 512;
  58.     
  59.     OSErr    theErr = noErr;
  60.     Size    theGrow;
  61.     Point    thePoint;
  62.     
  63.     gControl = theControl;
  64.     gStartValue = GetControlValue ( theControl );
  65.     
  66.     gValueSlop = 0;
  67.     GetMouse ( &thePoint );
  68.     gValueSlop = GetControlValue ( theControl ) - CalcValueFromPoint ( theControl, thePoint );    
  69.     
  70.     // Preflight memory for call to NewRgn
  71.     if ( MaxMem ( &theGrow ) < kMinLowMem )
  72.     {
  73.         theErr = memFullErr;
  74.         goto CleanupAndBail;
  75.     }
  76.     gSaveClip = NewRgn ( );
  77.     
  78.     DisableDrawing ( );
  79.     
  80. CleanupAndBail:
  81.     // Don't have to do any cleanup here
  82.     
  83.     return theErr;
  84. }
  85.  
  86.     
  87.  
  88. void EndThumbTracking ( void )
  89. {
  90.     EnableDrawing ( );
  91.     
  92.     DisposeRgn ( gSaveClip );
  93.     SetControlValue ( gControl, gSaveValue );
  94.     
  95.     return;
  96. }
  97.  
  98.  
  99.  
  100. pascal void ScrollThumbActionProc ( void )
  101. {
  102.     SInt32            theValue;
  103.     WindowRef        theWindow;
  104.     ControlRef        theControl = gControl;
  105.     Point            thePoint;
  106.     Rect            theRect;
  107.     tWindowInfoPtr    theInfo;
  108.     
  109.     
  110.     theWindow = (*theControl)->contrlOwner;
  111.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  112.     
  113.     #if DEBUGGING
  114.     if ( theInfo == nil ) DebugStr ( "\p theInfo == nil" );
  115.     #endif
  116.     
  117.      theRect = (*theControl)->contrlRect;
  118.      if ( theControl == theInfo->hScrollBar )
  119.         InsetRect ( &theRect, -kThumbTrackLengthSlop, -kThumbTrackWidthSlop );
  120.      else
  121.         InsetRect ( &theRect, -kThumbTrackWidthSlop, -kThumbTrackLengthSlop );
  122.     
  123.     // Assumes the port is correctly set up
  124.     GetMouse ( &thePoint );
  125.     if ( PtInRect ( thePoint, &theRect ) )
  126.         theValue = CalcValueFromPoint ( theControl, thePoint );
  127.     else
  128.         theValue = gStartValue;
  129.     
  130.     if ( theValue != GetControlValue ( theControl ) )
  131.     {
  132.         EnableDrawing ( );
  133.         
  134.         gSaveValue = theValue;
  135.          SetControlValue ( theControl, theValue );
  136.          UpdateWindowContent ( theWindow );
  137.          
  138.          DisableDrawing ( );
  139.          
  140.     }
  141.     
  142.     return;
  143. }
  144.  
  145.  
  146.  
  147. pascal void ScrollControlActionProc ( ControlRef theControl, SInt16 thePart )
  148. {
  149.     WindowRef        theWindow;
  150.     tWindowInfoPtr    theInfo;
  151.     SInt32            theValue,
  152.                     theDistance,
  153.                     limitValue;
  154.     
  155.     
  156.     theWindow = (*theControl)->contrlOwner;
  157.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  158.     
  159.     #if DEBUGGING
  160.     if ( theInfo == nil ) DebugStr ( "\p theInfo == nil" );
  161.     #endif
  162.     
  163.     
  164.     switch ( thePart )
  165.     {
  166.         case inUpButton:
  167.         case inDownButton:
  168.             theDistance = 10;
  169.         break;
  170.             
  171.         case inPageUp:
  172.         case inPageDown:
  173.             if ( theControl == theInfo->hScrollBar )
  174.                 theDistance = (theWindow->portRect.right - theWindow->portRect.left)
  175.                                                 - kScrollBarWidth - kPageOverlap;
  176.             else
  177.                 theDistance = (theWindow->portRect.bottom - theWindow->portRect.top)
  178.                                                 - kScrollBarWidth - kPageOverlap;
  179.         break;
  180.     }
  181.     
  182.     theValue = GetControlValue ( theControl );
  183.     
  184.     switch ( thePart )
  185.     {
  186.         case inUpButton:
  187.         case inPageUp:
  188.             limitValue = GetControlMinimum ( theControl );
  189.             if ( theValue - theDistance < limitValue )
  190.                 theValue = limitValue;
  191.             else
  192.                 theValue -= theDistance;
  193.         break;
  194.                 
  195.         case inDownButton:
  196.         case inPageDown:
  197.             limitValue = GetControlMaximum ( theControl );
  198.             if ( theValue + theDistance > limitValue )
  199.                 theValue = limitValue;
  200.             else
  201.                 theValue += theDistance;
  202.         break;
  203.     }
  204.     
  205.     SetControlValue ( theControl, theValue );
  206.     UpdateWindowContent ( theWindow );
  207.     
  208.     return;
  209. }
  210.  
  211.  
  212.  
  213. static SInt32 CalcValueFromPoint ( ControlRef theControl, Point thePoint )
  214. {
  215.     SInt32            theValue,
  216.                     theRange,
  217.                     theDistance,
  218.                     thePin;
  219.     Rect            theRect;
  220.     WindowRef        theWindow;
  221.     tWindowInfoPtr    theInfo;
  222.     
  223.     
  224.     theWindow = (*theControl)->contrlOwner;
  225.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  226.     
  227.     #if DEBUGGING
  228.     if ( theInfo == nil ) DebugStr ( "\p theInfo == nil" );
  229.     #endif
  230.         
  231.     theRect = (*theControl)->contrlRect;
  232.     theRange = GetControlMaximum ( theControl ) - GetControlMinimum ( theControl );
  233.     if ( theControl == theInfo->vScrollBar )
  234.     {
  235.         // Scroll distance adjusted for scroll arrows and the thumb
  236.         theDistance = theRect.bottom - theRect.top - kTotalWidthAdjust;
  237.         
  238.         // Pin thePoint to the middle of the thumb
  239.         thePin = theRect.top + (kTotalWidthAdjust / 2);
  240.         
  241.         theValue = ((thePoint.v - thePin) * theRange) / theDistance;
  242.     }
  243.     else
  244.     {
  245.         // Scroll distance adjusted for scroll arrows and the thumb
  246.         theDistance = theRect.right - theRect.left - kTotalWidthAdjust;
  247.         
  248.         // Pin thePoint to the middle of the thumb
  249.         thePin = theRect.left + (kTotalWidthAdjust / 2);
  250.         
  251.         theValue = ((thePoint.h - thePin) * theRange) / theDistance;
  252.     }
  253.     
  254.     theValue += gValueSlop;
  255.     
  256.     
  257.     if ( theValue < GetControlMinimum ( theControl ) )
  258.         theValue = GetControlMinimum ( theControl );
  259.     else if ( theValue > GetControlMaximum ( theControl ) )
  260.         theValue = GetControlMaximum ( theControl );
  261.     
  262.     return theValue;
  263. }
  264.  
  265.  
  266.  
  267. static void DisableDrawing ( void )
  268. {
  269.      Rect    nullRect = { 0, 0, 0, 0 };
  270.  
  271.     GetClip ( gSaveClip );
  272.     ClipRect ( &nullRect );
  273.     
  274.     return;
  275. }
  276.  
  277.  
  278.  
  279. static void EnableDrawing ( void )
  280. {
  281.     SetClip ( gSaveClip );
  282.     
  283.     return;
  284. }
  285.